package com.jltx.zeus.viewmodel.ext


import kotlin.reflect.KClass

/**
 * @author aidenquan
 *         Created by junlintianxia on 2021/08/20.
 *         用来记录需要传递的函数参数数据列表
 */
class ParameterList(vararg value: ParameterBean) {
    val values: MutableList<ParameterBean> = value.toMutableList()
    private fun <T> elementAt(i: Int): T =
        if (values.size > i) values[i] as T else throw IllegalStateException("Can't get parameter value #$i from $this")

    fun addElement(value: ParameterBean) {
        values.add(value)
    }

    fun lastElement(): ParameterBean? = if (values.size > 1) {
        values[values.size - 1]
    } else {
        null
    }

    fun size(): Int = values.size

    operator fun <T> component1(): T = elementAt(0)
    operator fun <T> component2(): T = elementAt(1)
    operator fun <T> component3(): T = elementAt(2)
    operator fun <T> component4(): T = elementAt(3)
    operator fun <T> component5(): T = elementAt(4)
    operator fun <T> component6(): T = elementAt(5)
    operator fun <T> component7(): T = elementAt(6)
    operator fun <T> component8(): T = elementAt(7)
    operator fun <T> component9(): T = elementAt(8)
    operator fun <T> component10(): T = elementAt(9)
    operator fun <T> component11(): T = elementAt(10)
}

/**
 * 重新给匿名lamada表达式()->ParameterList取一个别名，好作为函数参数传递去让外部返回一个参数List的数据对象
 */
typealias ParameterDefinition = () -> ParameterList

/**
 * Help build a Parameter list
 * @see ParameterList
 * return ParameterList
 */
fun parametersOf(vararg value: ParameterBean) = ParameterList(*value)

/**
 * Empty Parameter List 不传参数
 */
fun emptyParameterDefinition() = { -> ParameterList() }

inline fun <reified T> parameter(value: Any): ParameterBean =
    if (value !is T) {
        throw IllegalArgumentException(
            "value show instance of ${T::class}"
        )
    } else {
        ParameterBean(T::class, value)
    }

data class ParameterBean(val modelClass: KClass<*>, val value: Any)